package cn.edu.ujn.project.supermarket.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import cn.edu.ujn.project.system.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.edu.ujn.project.common.annotation.Log;
import cn.edu.ujn.project.common.core.controller.BaseController;
import cn.edu.ujn.project.common.core.domain.AjaxResult;
import cn.edu.ujn.project.common.core.page.TableDataInfo;
import cn.edu.ujn.project.common.enums.BusinessType;
import cn.edu.ujn.project.common.utils.poi.ExcelUtil;
import cn.edu.ujn.project.supermarket.domain.GoodsInfor;
import cn.edu.ujn.project.supermarket.domain.SellReport;
import cn.edu.ujn.project.supermarket.domain.VipUser;
import cn.edu.ujn.project.supermarket.service.IGoodsInforService;
import cn.edu.ujn.project.supermarket.service.ISellReportService;
import cn.edu.ujn.project.supermarket.service.IVipUserService;

/**
 * 购买界面Controller
 * 
 * @author zhaomh
 * @date 2023-11-13
 */
@Controller
@RequestMapping("/system/purchaseview")
public class PurchaseController extends BaseController
{
    private String prefix = "system/purchaseview";
    @Autowired
    private IGoodsInforService goodsInforService;
@Autowired
private IVipUserService vipUserService;
@Autowired 
private ISellReportService sellReportService;
    @Autowired
    private ISysRoleService sysRoleService;

    @RequiresPermissions("system:purchaseview:view")
    @GetMapping()
    public String purchaseview(ModelMap mmap)
    {
        mmap.put("userRole", this.sysRoleService.selectRolesByUserId(getUserId()).get(0).getRoleId());
        return prefix + "/purchaseview";
    }

    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("system:purchaseview:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsInfor goodsInfor)
    {
        startPage();
        List<GoodsInfor> list = goodsInforService.selectGoodsInforList(goodsInfor);

        return getDataTable(list);
    }

    /**
     * 销售商品
     */
    @PostMapping(value="/purchase")
    @ResponseBody
    public String purchase(@RequestBody String json, ModelMap mmap)
    {
    	System.out.println(json);
    	@SuppressWarnings("unchecked")

		HashMap<String,String> map = JSON.parseObject(json, HashMap.class );
      System.out.println(map);
      //获取余额
      String vipId=String.valueOf( map.get("vipId"));
      map.remove("vipId");
      VipUser vipUser = vipUserService.selectVipUserByVipId(vipId);
      double sum=0;
      for (String key : map.keySet()) {
    	    //计算总价
    	  GoodsInfor goodsInfor = goodsInforService.selectGoodsInforByBarCode(key);
    	  sum+=goodsInfor.getPrice()*Integer.valueOf(map.get(key));//计算总价
    	 
    	}
      System.out.println(sum);
      if(vipUser==null||vipUser.getBalance()<sum)//余额不足
      {
    	
    	  return "purchaseDefeat";
      }
      else {
    	  String sellId=tool.Generator.getString();
          for (String key : map.keySet()) {
      	    //添加购买记录和更改余量(购买量大于0
        	  if(Integer.valueOf(map.get(key))>0) {
      	  GoodsInfor goodsInfor = goodsInforService.selectGoodsInforByBarCode(key);
      	  sellReportService.insertSellReport(new SellReport(sellId,vipId,goodsInfor.getBarCode(),Integer.valueOf(map.get(key)),new Date()));
      	  goodsInfor.setQuantity(goodsInfor.getQuantity()-Integer.valueOf(map.get(key)));
      	  goodsInforService.updateGoodsInfor(goodsInfor);
      	}}
          //更新用户余额
          vipUser.setBalance(vipUser.getBalance()-sum);
          vipUserService.updateVipUser(vipUser);
      }
     
        return "purchaseSucceed";
    }
    
    /**
     * 导出商品信息列表
     */
    @RequiresPermissions("system:purchaseview:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsInfor goodsInfor)
    {
        List<GoodsInfor> list = goodsInforService.selectGoodsInforList(goodsInfor);
        ExcelUtil<GoodsInfor> util = new ExcelUtil<GoodsInfor>(GoodsInfor.class);
        return util.exportExcel(list, "商品信息数据");
    }



    /**
     * 新增保存商品信息
     */
    @RequiresPermissions("system:purchaseview:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsInfor goodsInfor)
    {
        return toAjax(goodsInforService.insertGoodsInfor(goodsInfor));
    }

    /**
     * 修改商品信息
     */
    @RequiresPermissions("system:purchaseview:edit")
    @PostMapping(value="/false")
    @ResponseBody
    public void off(@RequestBody String barCode, ModelMap mmap)
    {
        GoodsInfor goodsInfor = goodsInforService.selectGoodsInforByBarCode(barCode);
        goodsInfor.setCondition("false");
        goodsInforService.updateGoodsInfor(goodsInfor);
           }
    @RequiresPermissions("system:purchaseview:edit")
    @PostMapping(value="/true")
    @ResponseBody
    public void up(@RequestBody String barCode, ModelMap mmap)
    {
    	System.out.println("上级成功");
        GoodsInfor goodsInfor = goodsInforService.selectGoodsInforByBarCode(barCode);
        goodsInfor.setCondition("true");
        goodsInforService.updateGoodsInfor(goodsInfor);
           }
}
